﻿// -----------------------------------------------------------------------
// <copyright file="PauseScreen.cs" company="Diddly Squad">
// </copyright>
// -----------------------------------------------------------------------
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace DiddlySquat
{
	/// <summary>
	/// Class for displaying the main menu screen
	/// </summary>
	public class MenuScreen : Screen
	{
		//Background texture for the screen
		readonly Texture2D titleScreenBackground;

		private readonly SpriteFont font;

		private readonly List<MenuItem> menuItems;

		private readonly GraphicsDeviceManager graphicsDeviceManager;

		private int drawPositionY;

		public MenuItem SelectedMenuItem { get; private set; }

		private int menuIndexer;

		public bool IsGamePaused { get; set; }

		/// <summary>
		/// Initializes a new instance of the <see cref="MenuScreen"/> class.
		/// </summary>
		/// <param name="theContent">The content.</param>
		/// <param name="theScreenEvent">The screen event.</param>
		/// <param name="graphics">The graphics.</param>
		public MenuScreen(ContentManager theContent, EventHandler theScreenEvent, GraphicsDeviceManager graphics)
			: base(theScreenEvent)
		{
			//Load the background texture for the screen
			titleScreenBackground = theContent.Load<Texture2D>("GameScreen");
			font = theContent.Load<SpriteFont>("fonts/menufont");
			this.graphicsDeviceManager = graphics;
			drawPositionY = 150;
			this.menuIndexer = 0;
			this.menuItems = new List<MenuItem>();
			this.CreateMenu();
			this.IsGamePaused = false;
		}

		/// <summary>
		/// Updates the screen.
		/// </summary>
		/// <param name="theTime">The time.</param>
		public override void Update(GameTime theTime)
		{

			state = Keyboard.GetState();
			//Move up on the menu - check if goes around
			if (CheckKey(Keys.Up))
			{
				menuItems[this.menuIndexer].IsSelected = false;
				if (this.menuIndexer == 0)
					this.menuIndexer = menuItems.Count - 1;
				else
					this.menuIndexer--;

				this.SelectedMenuItem = menuItems[this.menuIndexer];
				this.SelectedMenuItem.IsSelected = true;
			}
			//Move down on the menu - check if goes around
			if (CheckKey(Keys.Down))
			{
				menuItems[this.menuIndexer].IsSelected = false;
				if (this.menuIndexer == menuItems.Count - 1)
					this.menuIndexer = 0;
				else
					this.menuIndexer++;

				this.SelectedMenuItem = menuItems[this.menuIndexer];
				this.SelectedMenuItem.IsSelected = true;

			}
			else if (CheckKey(Keys.Enter) || CheckKey(Keys.Space))
			{
				ScreenEvent.Invoke(this, new EventArgs());
			}

			base.Update(theTime);
			oldState = Keyboard.GetState();
		}

		/// <summary>
		/// Draws to the specified.
		/// </summary>
		/// <param name="theBatch">The batch.</param>
		public override void Draw(SpriteBatch theBatch)
		{
			
			theBatch.Draw(titleScreenBackground, Vector2.Zero, Color.White);

			drawPositionY = 100;
			
			foreach (MenuItem menuItem in menuItems)
			{
				Color itemColor = menuItem.IsSelected ? Constants.SelectedMenuItemColor : Constants.MenuItemColor;
				this.drawPositionY += font.LineSpacing;

				//Change new to continue if we are in the middle of the game
				if (IsGamePaused && menuItem.MenuItemType == Enums.MenuItemType.New) menuItem.Text = Constants.MenuContinue;
				
				theBatch.DrawString(font, menuItem.Text, new Vector2(graphicsDeviceManager.GraphicsDevice.Viewport.Width / 2 - (int)font.MeasureString(menuItem.Text).X, this.drawPositionY), itemColor);
			}

			base.Draw(theBatch);
		}

		/// <summary>
		/// Creates the menu.
		/// </summary>
		private void CreateMenu()
		{
			menuItems.Add(new MenuItem(Constants.MenuNew, Enums.MenuItemType.New));
			menuItems.Add(new MenuItem(Constants.MenuLoad, Enums.MenuItemType.Load));
			menuItems.Add(new MenuItem(Constants.MenuSave, Enums.MenuItemType.Save));
			menuItems.Add(new MenuItem(Constants.MenuOptions, Enums.MenuItemType.Options));
			menuItems.Add(new MenuItem(Constants.MenuHelp, Enums.MenuItemType.Help));
			menuItems.Add(new MenuItem(Constants.MenuCredits, Enums.MenuItemType.Credits));
			menuItems.Add(new MenuItem(Constants.MenuQuit, Enums.MenuItemType.Quit));
			menuItems[0].IsSelected = true;
			this.SelectedMenuItem = menuItems[0];
		}
	}
}
